package com.fernfx.xingtan.user.ui;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.RongYun;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.main.ui.MainActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.UIUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 应用程序启动 欢迎页面
 */
public class WelcomeActivity extends BaseActivity implements View.OnClickListener {

    private Handler welcomeHandler = initHandler();
    private static final int WELCOME_HANDLER_STATUS = 0;

    @BindView(R.id.skip_countdown_tv)
    TextView skipCountdownTv;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_user_welcome;
    }

    @Override
    protected void initData() {
        Message timeMessage = welcomeHandler.obtainMessage();
        timeMessage.what = WELCOME_HANDLER_STATUS;
        timeMessage.arg1 = 3000;
        welcomeHandler.sendMessage(timeMessage);
    }

    @OnClick({R.id.skip_countdown_tv})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.skip_countdown_tv:
                Message timeMessage = welcomeHandler.obtainMessage();
                timeMessage.what = WELCOME_HANDLER_STATUS;
                timeMessage.arg1 = -1;
                welcomeHandler.removeMessages(WELCOME_HANDLER_STATUS);
                welcomeHandler.sendMessage(timeMessage);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        welcomeHandler.removeMessages(WELCOME_HANDLER_STATUS);
        welcomeHandler = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (AccountManager.isLogin() && null != AccountManager.getUserInfo()) {
            MemberEntity memberEntity = AccountManager.getUserInfo();
            RongYun.connectRongYun(this, memberEntity, memberEntity.getObj().getRongCloudToken(), false);
        }
    }

    /*
     * init handler
     */
    private final Handler initHandler() {

        return new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                int time = msg.arg1;
                if (time < 0 && AccountManager.isLogin()) {
                    MainActivity.start(mContext);
                    finish();
                } else if (time < 0 && !AccountManager.isLogin()) {
                    LoginActivity.start(mContext);
                    finish();
                } else {
                    //TextView text
                    String skipCountText = skipCountdownTv.getText().toString();
                    String ftSkipCountText = String.format(UIUtil.getString(R.string.welcome_countdown), time / 1000);
                    skipCountdownTv.setText(ftSkipCountText);
                    //send message
                    Message timeMessage = welcomeHandler.obtainMessage();
                    timeMessage.what = WELCOME_HANDLER_STATUS;
                    timeMessage.arg1 = time - 1000;
                    welcomeHandler.sendMessageDelayed(timeMessage, 1000);
                }
            }
        };
    }
}
